<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/18 0018
 * Time: 09:20
 */

namespace app\admin\controller\mk;
/*订单管理*/

use app\admin\Controller;
use app\common\service\AddressService;
use app\common\service\OrdersService;
use app\common\service\RefundService;
use think\Exception;


\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);


class Orders extends Controller
{
    use \app\admin\traits\controller\Controller;

    public function index(){
        $param = $this->request->param();
        $service = new OrdersService();//数据处理
        $service->searchPage($this->view,$param);
        //$this->view->assign('state',getOrderState());
        return $this->view->fetch();
    }

    public function beforeEdit(){
        $orderService = new OrdersService();
        $aService = new AddressService();
        $orderGoods = $orderService->getOrderGoods($this->request->param('id'));
        $address = $aService->getUserAddress($orderGoods['sms_address_id']);
        $this->view->assign('goods',$orderGoods['goods']);
        $this->view->assign('address',$address);//发货地址
    }


    public function confirmChangeState(){
        $orderId = $this->request->param('id');
        if(empty($orderId))ajax_return_adv_error('订单丢失！');
        $newState = $this->request->param('new_state');
        $eMsg = '';
        try {
            switch ($newState){
                case 'received':
                    $service = new OrdersService();//数据处理
                    $result = $service->editState($orderId,'paid','received');
                    if($result) return ajax_return_adv('发货成功');
                    break;
                case 'refunded':
                    //$this->confirmRefundMoney();
                    $rService = new RefundService($orderId);
                    $rService->refundMoney();
                    return ajax_return_adv('退款成功');
                    break;
                case 'refundback':
                    $service = new OrdersService();//数据处理
                    $result = $service->editState($orderId,'refund','refundback',false);
                    if($result) return ajax_return_adv('操作成功');
            }

        }catch (Exception $e){
            $eMsg = $e->getMessage();
        }
        return ajax_return_adv_error($eMsg?$eMsg:'操作失败！');
    }

    /*public function confirmRefundMoney(){
        $orderId = $this->request->param('id');

    }*/


    /**
     * @return string
     * @throws \think\Exception 需求列表
     */
    public function requires(){
        $param = $this->request->param();
        $service = new OrdersService();//数据处理
        $service->requirePage($this->view,$param);
        return $this->view->fetch();
    }

    /**
     * 需求处理
     */
    public function requiresCl(){
        if ($this->request->isAjax()) {
            //更新
            $orderService = new OrdersService();
            return $orderService->requireGoods($this->request->param());
        } else {
            // 编辑
            //返回店铺
            $orderService = new OrdersService();
            $orderGoods = $orderService->getOrderGoods($this->request->param('id'));
            $this->view->assign("vo", $orderGoods);
            return $this->view->fetch();
        }
    }


}